//
//  combatViewController.m
//  Project
//
//  Created by iD Student on 7/31/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "combatViewController.h"

@interface combatViewController ()

@end

@implementation combatViewController
@synthesize yourSoldiers;
@synthesize yourArchers;
@synthesize yourCavalry;
@synthesize yourCatapult;
@synthesize enemySoldiers;
@synthesize enemyArchers;
@synthesize enemyCavalry;
@synthesize enemyCatapult;
@synthesize enemyStr;
@synthesize allyStr;
@synthesize label1;
@synthesize label2;
@synthesize label3;
@synthesize combatButton;
@synthesize lossFighters;
@synthesize lossArchers;
@synthesize lossCavalry;
@synthesize lossCatapult;
@synthesize plunder1;
@synthesize plunder2;
@synthesize plunder3;
@synthesize plunderAmt;
@synthesize leaveCombat;
@synthesize endLabel;
@synthesize beginLabel;
@synthesize victory;
@synthesize succeeded;
@synthesize combatLoss;
@synthesize endGame;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    if (declareWar == TRUE)
    {
        enySoldiers = (arc4random() %(5 + day) + day) + 5;
        enyArchers = ((arc4random() %(3 + day) + day) / 2) + 5;
        enyCavalry = ((arc4random() %(2 + day) + day) / 4);
        enyCatapult = ((arc4random() %(1 + day) + day) / 8);
    }
    else 
    {
        enySoldiers = (arc4random() %(5) + (day / 3));
        enyArchers = (arc4random() %(3) + (day / 4));
        enyCavalry = (arc4random() %(2) + (day / 10));
        enyCatapult = (arc4random() %(1 + (day / 25)) + (day / 25));
    }
    plunder1.hidden = TRUE;
    plunder2.hidden = TRUE;
    plunder3.hidden = TRUE;
    plunderAmt.text = @"";
    leaveCombat.hidden = TRUE;
    endLabel.hidden = TRUE;
    victory.hidden = TRUE;
    succeeded.hidden = TRUE;
    
    plunderAmt.hidden = TRUE;
    label1.hidden = TRUE;
    label2.hidden = TRUE;
    allyStr.hidden = TRUE;
    enemyStr.hidden = TRUE;
    
    
    yourSoldiers.text = [NSString stringWithFormat: @"%d", fighterAmt];
    yourArchers.text = [NSString stringWithFormat: @"%d", archerAmt];
    yourCavalry.text = [NSString stringWithFormat: @"%d", cavalryAmt];
    yourCatapult.text = [NSString stringWithFormat: @"%d", catapultAmt];
    enemySoldiers.text = [NSString stringWithFormat: @"%d", enySoldiers];
    enemyArchers.text = [NSString stringWithFormat: @"%d", enyArchers];
    enemyCavalry.text = [NSString stringWithFormat: @"%d", enyCavalry];
    enemyCatapult.text = [NSString stringWithFormat: @"%d", enyCatapult];
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setYourSoldiers:nil];
    [self setYourArchers:nil];
    [self setYourCavalry:nil];
    [self setYourCatapult:nil];
    [self setEnemySoldiers:nil];
    [self setEnemyArchers:nil];
    [self setEnemyCavalry:nil];
    [self setEnemyCatapult:nil];
    [self setEnemyStr:nil];
    [self setAllyStr:nil];
    [self setLabel1:nil];
    [self setLabel2:nil];
    [self setCombatButton:nil];
    [self setLossFighters:nil];
    [self setLossArchers:nil];
    [self setLossCavalry:nil];
    [self setLossCatapult:nil];
    [self setLabel3:nil];
    [self setPlunder1:nil];
    [self setPlunder2:nil];
    [self setPlunder3:nil];
    [self setPlunderAmt:nil];
    [self setLeaveCombat:nil];
    [self setEndLabel:nil];
    [self setBeginLabel:nil];
    [self setVictory:nil];
    [self setSucceeded:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)back:(id)sender
{
    lossFighters.text = @"Fighters:";
    lossArchers.text = @"Archers:";
    lossCavalry.text = @"Cavalry:";
    lossCatapult.text = @"Catapults:";
    lostFighters = 0;
    lostArchers = 0;
    lostCavalry = 0;
    lostCatapults = 0;
    plunder1.hidden = TRUE;
    plunder2.hidden = TRUE;
    plunder3.hidden = TRUE;
    plunderAmt.text = @"";
    enemySoldiers.hidden = FALSE;
    enemyArchers.hidden = FALSE;
    enemyCavalry.hidden = FALSE;
    enemyCatapult.hidden = FALSE;
    label3.hidden = FALSE;
    leaveCombat.hidden = TRUE;
    losses = 0;
    plunderAmt.hidden = TRUE;
    label1.hidden = TRUE;
    label2.hidden = TRUE;
    allyStr.hidden = TRUE;
    enemyStr.hidden = TRUE;
    victory.hidden = TRUE;
    declareWar = FALSE;
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)beginCombat:(id)sender 
{
    allyStrength = 0;
    enemyStrength = 0;
    
    if (offense == TRUE)
    {
        for (y = fighterAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(5) + 9);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (raiding == TRUE)
            {
                allyStrength = allyStrength + 1;
            }
        }
        for (y = archerAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(4) + 1);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (raiding == TRUE)
            {
                allyStrength = allyStrength + 1;
            }
        }
        for (y = cavalryAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(5) + 19);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (raiding == TRUE)
            {
                allyStrength = allyStrength + 1;
            }
        }
        for (y = catapultAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(9)+ 49);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (raiding == TRUE)
            {
                allyStrength = allyStrength + 1;
            }
        }
        
        for (y = enySoldiers; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(3) + 7);
        }
        for (y = enyArchers; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(5) + 13);
        }
        for (y = enyCavalry; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(2) + 4);
        }
        for (y = enyCatapult; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(3)+ 7);
        }
        
        combatButton.hidden = TRUE;
        beginLabel.hidden = TRUE;
        leaveCombat.hidden = FALSE;
        endLabel.hidden = FALSE;
        offense = FALSE;
        
        if (allyStrength >= enemyStrength)
        {
            label1.text = @"Ally Strength";
            label2.text = @"Enemy Strength";
            allyStr.text = [NSString stringWithFormat: @"%d", allyStrength];
            enemyStr.text = [NSString stringWithFormat: @"%d", enemyStrength];
            
            victory.hidden = FALSE;
            plunderAmt.hidden = FALSE;
            label1.hidden = FALSE;
            label2.hidden = FALSE;
            allyStr.hidden = FALSE;
            enemyStr.hidden = FALSE;
            plunder1.hidden = FALSE;
            plunder2.hidden = FALSE;
            plunder3.hidden = FALSE;
            if (raiding == TRUE)
            {
                plunderAmt.text = [NSString stringWithFormat:@"%d", (fighterAmt*19) + (archerAmt*11) + (cavalryAmt*41) + (catapultAmt*5)];
                wood = wood + (fighterAmt*19) + (archerAmt*11) + (cavalryAmt*41) + (catapultAmt*5);
                stone = stone + (fighterAmt*19) + (archerAmt*11) + (cavalryAmt*41) + (catapultAmt*5);
                iron = iron + (fighterAmt*19) + (archerAmt*11) + (cavalryAmt*41) + (catapultAmt*5);
            }
            else 
            {
                plunderAmt.text = [NSString stringWithFormat:@"%d", (fighterAmt*12) + (archerAmt*7) + (cavalryAmt*34) + (catapultAmt*3)];
                wood = wood + (fighterAmt*12) + (archerAmt*7) + (cavalryAmt*34) + (catapultAmt*3);
                stone = stone + (fighterAmt*12) + (archerAmt*7) + (cavalryAmt*34) + (catapultAmt*3);
                iron = iron + (fighterAmt*12) + (archerAmt*7) + (cavalryAmt*34) + (catapultAmt*3);
            }
            
            if (declareWar == TRUE)
            {
                victor++;
                declareWar = FALSE;
                succeeded.hidden = FALSE;
                if (victor == 10)
                {
                    endGame = [self.storyboard instantiateViewControllerWithIdentifier:@"endGame"];
                    [self presentModalViewController: endGame animated:YES];
                }
            }
        }
        else
        {
            label1.hidden = FALSE;
            label2.hidden = FALSE;
            allyStr.hidden = FALSE;
            enemyStr.hidden = FALSE;
            label1.text = @"Ally Strength";
            label2.text = @"Enemy Strength";
            allyStr.text = [NSString stringWithFormat: @"%d", allyStrength];
            enemyStr.text = [NSString stringWithFormat: @"%d", enemyStrength];
            
            losses = (((float) enemyStrength / (float) allyStrength) - 1) * 100;
            for (z = fighterAmt; z > 0; z--)
            {
                y = (arc4random() %(100));
                if (y <= losses)
                {
                    fighterAmt--;
                    lostFighters++;
                }
            }
            for (z = archerAmt; z > 0; z--)
            {
                y = (arc4random() %(100));
                if (y <= losses)
                {
                    archerAmt--;
                    lostArchers++;
                }
            }
            for (z = cavalryAmt; z > 0; z--)
            {
                y = (arc4random() %(100));
                if (y <= losses)
                {
                    cavalryAmt--;
                    lostCavalry++;
                }
            }
            for (z = catapultAmt; z > 0; z--)
            {
                y = (arc4random() %(100));
                if (y <= losses)
                {
                    catapultAmt--;
                    lostCatapults++;
                }
            }
            
            lossFighters.text = [NSString stringWithFormat: @"%d", 0 - lostFighters];
            lossArchers.text = [NSString stringWithFormat: @"%d", 0 - lostArchers];
            lossCavalry.text = [NSString stringWithFormat: @"%d", 0 - lostCavalry];
            lossCatapult.text = [NSString stringWithFormat: @"%d", 0 - lostCatapults];
            enemySoldiers.hidden = TRUE;
            enemyArchers.hidden = TRUE;
            enemyCavalry.hidden = TRUE;
            enemyCatapult.hidden = TRUE;
            label3.hidden = TRUE;
        }
    }
    else
    {
        for (y = fighterAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(3) + 7);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (cityBarricade == TRUE)
            {
                allyStrength = allyStrength + 2;
            }
        }
        for (y = archerAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(5) + 13);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (cityBarricade == TRUE)
            {
                allyStrength = allyStrength + 2;
            }
        }
        for (y = cavalryAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(2) + 4);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (cityBarricade == TRUE)
            {
                allyStrength = allyStrength + 2;
            }
        }
        for (y = catapultAmt; y > 0; y--)
        {
            allyStrength = allyStrength + (arc4random() %(3)+ 7);
            if (valiantSoldiers == TRUE)
            {
                allyStrength++;
            }
            if (cityBarricade == TRUE)
            {
                allyStrength = allyStrength + 2;
            }
        }
    
        for (y = enySoldiers; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(5) + 9);
        }
        for (y = enyArchers; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(4) + 1);
        }
        for (y = enyCavalry; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(5) + 19);
        }
        for (y = enyCatapult; y > 0; y--)
        {
            enemyStrength = enemyStrength + (arc4random() %(9)+ 49);
        }
    
        combatButton.hidden = TRUE;
        beginLabel.hidden = TRUE;
        leaveCombat.hidden = FALSE;
        endLabel.hidden = FALSE;
        
        if (allyStrength > enemyStrength)
        {
            victory.hidden = FALSE;
            label1.hidden = FALSE;
            label2.hidden = FALSE;
            allyStr.hidden = FALSE;
            enemyStr.hidden = FALSE;
            label1.text = @"Ally Strength";
            label2.text = @"Enemy Strength";
            allyStr.text = [NSString stringWithFormat: @"%d", allyStrength];
            enemyStr.text = [NSString stringWithFormat: @"%d", enemyStrength];
        }
        else
        {
            combatLoss = [self.storyboard instantiateViewControllerWithIdentifier:@"combatLoss"];
            [self presentModalViewController: combatLoss animated:YES];
        }
    }
}
- (void)dealloc {
    [yourSoldiers release];
    [yourArchers release];
    [yourCavalry release];
    [yourCatapult release];
    [enemySoldiers release];
    [enemyArchers release];
    [enemyCavalry release];
    [enemyCatapult release];
    [enemyStr release];
    [allyStr release];
    [label1 release];
    [label2 release];
    [combatButton release];
    [lossFighters release];
    [lossArchers release];
    [lossCavalry release];
    [lossCatapult release];
    [label3 release];
    [plunder1 release];
    [plunder2 release];
    [plunder3 release];
    [plunderAmt release];
    [leaveCombat release];
    [endLabel release];
    [beginLabel release];
    [victory release];
    [succeeded release];
    [super dealloc];
}
@end
